import { history } from 'umi';
import { useMemo, useState } from 'react';
import { Form } from 'antd';
import { setTableRowClassName } from './Format';
import { ChangeMethodManageService } from '../services/ManageService';

import {
  getProcurementMethodManageTableColumns,
  getProcurementMethodManageSearchColumns,
  getProcurementMethodManageTableHeaderActions,
} from '../mconfig/index';

import { getKeyRouter } from '@/utils';

/**
 * @useNeedManage 管理页hooks
 * @param curRes 权限信息
 * @returns
 */
export function useProcurementMethodManage({ curRes, locationProps }: Record<string, any>) {
  // 作废申请表单实例
  const [cancelForm] = Form.useForm();
  // 作废申请渲染状态
  const [isShowCancel, setIsShowCancel] = useState<Record<string, any>>({});
  const onChangeCancelModalState = (resObj: Record<string, any>) => setIsShowCancel(resObj);

  const [invalidState, setInvalidState] = useState<Record<string, any>>({}); /**撤销弹窗*/

  // API 接口
  const { getChangeMethodManageService } = ChangeMethodManageService();
  const {
    changeMethodManageRun,
    changeMethodManageDataSource,
    changeMethodManageLoading,
    changeMethodManageTableProps,
    reloadRequest,
  } = getChangeMethodManageService(curRes);
  /**作废事件 */
  const submitCancel = () => {
    return new Promise(async (resolve) => {
      const result = await cancelForm.validateFields();
      resolve(true);
    });
  };

  // 管理页操作事件
  const operation = {
    // onCheck: (record: Record<string, any>) => {
    //   history.push({
    //     pathname: `/purchaseSourceChangeController/add/:${getKeyRouter()}${getKeyRouter()}`,
    //     state: {
    //       info: record,
    //       type: 'check',
    //     },
    //   });
    // },
    onAdd: (record: Record<string, any>) => {
      history.push({
        pathname: `/purchaseSourceChangeController/add/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          info: {},
          type: 'add',
          returnPathName: locationProps?.pathname,
        },
      });
    },
    onView: (record: Record<string, any>) => {
      history.push({
        pathname: `/purchaseSourceChangeController/getSourceChangeById/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          info: { changeID: record?.id, applyTitle: record?.purchProjectName, packName: record?.packName },
          type: 'view',
          returnPathName: locationProps?.pathname,
        },
      });
    },
    onEdit: (record: Record<string, any>) => {
      history.push({
        pathname: `/purchaseSourceChangeController/edit/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          info: { changeID: record?.id, applyTitle: record?.purchProjectName, packName: record?.packName },
          type: 'edit',
          returnPathName: locationProps?.pathname,
        },
      });
    },
    onCancel: (record: Record<string, any>) => {
      onChangeCancelModalState({ isShow: true, resObj: record });
    },
    onInvalid: (record) => {
      setInvalidState({ isShow: true, resObj: record });
    },
  };

  //头部权限切换
  const switchList = useMemo(() => {
    // return ProcurementMethodManageTabSwitch(curRes);
    return [];
  }, [curRes]);

  // 表格配置
  const tableProps: Record<string, any> = {
    columns: getProcurementMethodManageTableColumns({ operation }, curRes),
    dataSource: changeMethodManageDataSource,
    rowClassName: setTableRowClassName,
    loading: changeMethodManageLoading,
    pagination: changeMethodManageTableProps?.pagination,
    rowKey: 'id',
    onPageChange: (current: number, pageSize: number) => {
      changeMethodManageRun({ current, pageSize });
    },
  };

  // 表格顶部查询条件配置
  const searchProps = {
    switchList: switchList,
    onSwitch: (_: string | undefined | number, searchValues: any, params: any) => {
      changeMethodManageRun({ ...searchValues, ...params });
    },
    searchList: getProcurementMethodManageSearchColumns(),
    onSearch: (request) => {
      changeMethodManageRun({ ...request });
    },
    onReset: (values) => {
      changeMethodManageRun({ ...values });
    },
  };
  // 表格顶部按钮配置
  const actionList = getProcurementMethodManageTableHeaderActions(operation, curRes);
  return {
    operation,
    tableProps,
    searchProps,
    actionList,
    cancelForm,
    isShowCancel,
    onChangeCancelModalState,
    submitCancel,
    invalidState,
    setInvalidState,
    reloadRequest,
  };
}
